<?php
class Promosi_model extends CI_Model{

	function   __construct() {
        parent::__construct();
    }

	private $tbl = 'promo10';

	
 function selectWeek(){

	   $query = $this->db->query("select * from week order by intid_week asc");
	   return $query->result();
    }

    function getPromo(){

	   $query = $this->db->query("SELECT a. * , b.strnama, c.strnama as nama
            FROM promo10 a, barang b, barang c
            WHERE a.intid_barang = b.intid_barang
                   AND a.intid_barang_free = c.intid_barang
            ORDER BY intid_promo ASC"
              );
	   return $query->result();
    }
   

    function insert($data){

       
	$data = array(
            'intid_week_start' => $this->input->post('intid_week_start'),
            'intid_week_end' => $this->input->post('intid_week_end'),
            'intid_barang' => $this->input->post('intid_barang'),
            'intid_barang_free' => $this->input->post('intid_barang_free')
			);
        $this->db->insert('promo10', $data);
	}

   function delete($idpromo){
       $this->db->where('intid_promo', $idpromo);
       $this->db->delete('promo10');
    }
    function select($idpromo){
       $query = $this->db->query("select a.*, b.strnama, c.strnama as nama 
		from promo10 a, barang b, barang c
		where a.intid_promo = $idpromo
		and a.intid_barang = b.intid_barang
		and a.intid_barang_free = c.intid_barang");
	   return $query->result();
    }

	function update($idpromo){

            $data = array(
            'intid_week_start' => $this->input->post('intid_week_start'),
            'intid_week_end' => $this->input->post('intid_week_end'),
           'intid_barang' => $this->input->post('intid_barang'),
            'intid_barang_free' => $this->input->post('intid_barang_free')
		);
            $this->db->where('intid_promo', $idpromo);
            $this->db->update('promo10',$data);
    }
	
	 function selectBarang($keyword){
        $query = $this->db->query("select intid_barang, upper(strnama) strnama from barang where strnama like '$keyword%'");
        return $query->result();
	}
	function selectBarangFree($keyword){
        $query = $this->db->query("select intid_barang, upper(strnama) strnama from barang where strnama like '$keyword%'");
        return $query->result();
	}
/********Promosi 20**************************/
 function getPromo20(){

	   $query = $this->db->query("SELECT a. * , b.strnama, c.strnama as nama
            FROM promo20 a, barang b, barang c
            WHERE a.intid_barang = b.intid_barang
                   AND a.intid_barang_free = c.intid_barang
            ORDER BY intid_promo ASC"
              );
	   return $query->result();
    }
     //page
	private $tbl2 = 'promo20';

	
          function insert2($data){


	$data = array(

            'intid_week_start' => $this->input->post('intid_week_start'),
            'intid_week_end' => $this->input->post('intid_week_end'),
            'intid_barang' => $this->input->post('intid_barang'),
            'intid_barang_free' => $this->input->post('intid_barang_free'),
            'intid_barang_free1' => $this->input->post('intid_barang_free1'),
	    'intid_barang_free2' => $this->input->post('intid_barang_free2')
			);
        $this->db->insert('promo20', $data);
	}


     function select2($idpromo2){
       $query = $this->db->query("select a.*, b.strnama, c.strnama as nama 
		from promo20 a, barang b, barang c
		where a.intid_promo = $idpromo2
		and a.intid_barang = b.intid_barang
		and a.intid_barang_free = c.intid_barang");
	   return $query->result();
     }

	function update2($idpromo){


            $data = array(
            'intid_week_start' => $this->input->post('intid_week_start'),
            'intid_week_end' => $this->input->post('intid_week_end'),
           'intid_barang' => $this->input->post('intid_barang'),
            'intid_barang_free' => $this->input->post('intid_barang_free'),
            'intid_barang_free1' => $this->input->post('intid_barang_free1'),
            'intid_barang_free2' => $this->input->post('intid_barang_free2')
		);
            $this->db->where('intid_promo', $idpromo);
            $this->db->update('promo20',$data);
    }
     function delete2($idpromo){
       $this->db->where('intid_promo', $idpromo);
       $this->db->delete('promo20');
    }

    //test az
        function Cari_promo10($limit,$offset,$strnama)
	{
	    $q = $this->db->query("SELECT a. * , b.strnama, c.strnama as nama
            FROM promo10 a, barang b, barang c
            WHERE a.intid_barang = b.intid_barang
            AND a.intid_barang_free = c.intid_barang
			AND b.strnama LIKE '%$strnama%' LIMIT $offset,$limit");
	    return $q;
           
           }

	function tot_hal($tabel,$field,$kata)
	{
	    $q = $this->db->query("select * from $tabel where $field like '%$kata%'");
	    return $q;
	}
//promo20
    function Cari_promo20($limit,$offset,$strnama)
	{
	    $q = $this->db->query("SELECT a. * , b.strnama, c.strnama as nama
            FROM promo20 a, barang b, barang c
            WHERE a.intid_barang = b.intid_barang
            AND a.intid_barang_free = c.intid_barang
			AND b.strnama LIKE '%$strnama%' LIMIT $offset,$limit");
	    return $q;

    }

	function tot_hal2($tabel,$field,$kata)
	{
	    $q = $this->db->query("select * from $tabel where $field like '%$kata%'");
	    return $q;
	}
//end of test
	//digunakan untuk PROMOSI/PROMOSI_STARTTERKIT_ABO
	function get_Promosi_starterkit_ABO($data){
		if(isset($data['intid_starterkit']) and isset($data['intid_week'])){
		$select = 'select * from member where member.intid_starterkit = "'.$data['intid_starterkit'].'" and intid_week = "'.$data['intid_week'].'" and intid_cabang = "'.$data['intid_cabang'].'" order by member.datetanggal asc';
		//echo $select;
		}
		$query	=	$this->db->query($select);
		return $query;
	}
	function get_omset_week($data){
		$query = $this->db->query('select sum(inttotal_omset) inttotal_omset from nota where intid_week = '.$data['intid_week'].' and intid_dealer = '.$data['intid_dealer'].' and is_dp = 0');
		return $query;	
	}
	function get_nota_dealer($data){
		$select = "select * from nota where intid_dealer = '".$data['intid_dealer']."' order by nota.datetgl asc";
		return $this->db->query($select);
	}
	function  get_dealer($data){
		$select  = "select *,(select intid_dealer from member m where m.strkode_dealer = member.strkode_upline )intid_upline  from member where intid_dealer = '".$data['intid_dealer']."'";
		return $this->db->query($select);
	}
	function update_nota($data){
		$update	=	"update nota set intid_dealer = '".$data['intid_dealer']."' where intid_nota = '".$data['intid_nota']."'";
		//echo $update."<br />";
		$this->db->query($update);
	}
	function insert_nota_history_from_nota($dataTemp){
			$select = "select * from nota where intid_nota = ".$dataTemp['intid_nota']."";
			$query = $this->db->query($select);
		$ket = "NOTA ";
		foreach($query->result() as $row){
			$ket .= "(".$row->intid_nota.", ".$row->intno_nota.", ".$row->intid_jpenjualan.", ".$row->intid_cabang.", ".$row->intid_dealer.", ".$row->intid_unit.", ".$row->datetgl.", ".$row->intid_week.", ".$row->intomset10.", ".$row->intomset20.", ".$row->inttotal_omset.", ".$row->inttotal_bayar.", ".$row->intdp.", ".$row->intcash.", ".$row->intdebit.", ".$row->intkkredit.", ".$row->intsisa.", ".$row->intkomisi10.", ".$row->intkomisi20.", ".$row->intpv.", ".$row->intvoucher.", ".$row->is_dp.", ".$row->inttrade_in.", ".$row->is_lg.", ".$row->nokk.", ".$row->is_asi.", ".$row->intkomisi_asi.", ".$row->is_arisan.", ".$row->halaman.")";
			}
			$ket1 = " NOTA_DETAIL ( ";
			$select2 = "select * from nota_detail where intid_nota = ".$row->intid_nota."";
			$query2 = $this->db->query($select2);
			foreach($query2->result() as $row){
					$ket1 .= "(".$row->intid_detail_nota.", ".$row->intid_nota.", ".$row->intid_barang.", ".$row->intquantity.", ".$row->intid_harga.", ".$row->is_free.", ".$row->intharga.") ";
					}
			$ket1 .= ") ";
		//echo $ket.$ket1."<br />";
		$selectall = "insert into nota_history (status,keterangan_history) values ('DELETE','".$ket.$ket1."');";
		$this->db->query($selectall);
		//echo $selectall."<br />";
	}
	//function de
	function delete_member($data){
	$delete = 'delete from member where intid_dealer = "'.$data['intid_dealer'].'"';
	//echo $delete."<br />";
	$this->db->query($delete);
	}
	//end of promosi building
}
?>
